package com.asit.demo.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.asit.demo.config.DynamicDatasource;
import com.asit.demo.mapper.UserMapper;
import com.asit.demo.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private UserMapper userMapper;
    @Resource
    private DynamicDatasource dynamicDatasource;

    @GetMapping("/save/{age}")
    public String saveUser(@PathVariable Integer age) {
        if (userMapper.insert(new User().setName("测试1").setAge(age))>0){
            return "添加成功!";
        }else{
            return "添加失败";
        }
    }

    @GetMapping("/update/{id}/{age}")
    public String updateUser(@PathVariable Integer id, @PathVariable Integer age) {
        User user = userMapper.selectById(id);
        if (user != null) {
            //user.setId(id).setVersion(3);
            user.setAge(age);
            if (userMapper.updateById(user) > 0) {
                return "更新成功!";
            }else{
                return "更新失败";
            }
        }
        return "无此用户";
    }

    /**
     * 根据ID查找
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public String getUserById(@PathVariable Integer id) {
        /*
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
        wrapper.ge(User::getId,id);
        wrapper.likeRight(User::getName,"测试");
        */
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.ge("id",id);
        wrapper.likeRight("name","测试");
        List<User> list = userMapper.selectList(wrapper);
        log.info("{}",list.size());
        if(list!=null&&list.size()>0){
            list.forEach(item-> {
                log.info("{}",item);
            });
        }
        //log.info("{}",dynamicDatasource.demo(userMapper,1));
        //log.info("{}",userMapper.selectById("salve1",id));
        wrapper = new QueryWrapper();
        wrapper.eq("id",2).select("id","name","age","createTime");
        User user1 = userMapper.selectOne(wrapper);
        log.info("{}",user1);
        User user = userMapper.selectById(id);
        return "hello " + user.getName();
    }
    @GetMapping("/selectList/{id}")
    public JSON selectList(@PathVariable Integer id) {
        /*
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
        wrapper.ge(User::getId,id);
        wrapper.likeRight(User::getName,"测试");
        */
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.ge("id",id)//>=
        .likeRight("name","测试");//like
        List<User> list = userMapper.selectList(wrapper);
        log.info("{}",list.size());
        if(list!=null&&list.size()>0){
            list.forEach(item-> {
                log.info("{}",item);
            });
        }
        return JSONUtil.parse(list);
    }

    @GetMapping("/selectPage/{currentPage}/{pageSize}")
    public JSON selectPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize) {
        /*
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
        wrapper.ge(User::getId,id);
        wrapper.likeRight(User::getName,"测试");
        */
        Page<User> page = new Page<>(currentPage,pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.select("name","age password1","(age+1) age","date_format(createtime, '%Y-%m-%d %H:%I:%S') createtime");
        wrapper.ge("id",1);//>=1
        wrapper.orderByDesc("id");
        //wrapper.likeRight("name","测试");//like
        userMapper.selectPage(page, wrapper);
        List<User> records = page.getRecords();
        log.info("{}",records.size());
        if(records!=null&&records.size()>0){
            records.forEach(item-> {
                log.info("{}",item);
            });
        }
        return JSONUtil.parse(records);
    }
    /**
     * 根据name查找
     *
     * @param name
     * @return
     */
    @GetMapping("/name/{name}")
    public User getUserByName(@PathVariable String name) {
        /*
        AdminServiceCglibProxy proxyFactory = new AdminServiceCglibProxy(userMapper);
        UserMapper proxy = (UserMapper)proxyFactory.getProxyInstance();
        log.info("{}",proxy.selectById(2));
        */
        log.info("{}", dynamicDatasource.demo(userMapper, 2));
        User user = userMapper.findUserByName(name);
        return user;
    }

    @GetMapping("/delete/{id}")
    public String deleteUserById(@PathVariable String id) {
        if(userMapper.deleteById(id)>0){
            return id+"删除成功";
        }else{
            return id+"删除失败";
        }
    }
}
